package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.constructionproject.api.dto.request.ProjectCountQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.request.ProjectListQueryRequest;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectResp;
import com.smedi.ismedi.constructionproject.api.dto.response.resp.ProjectWithDingResp;
import com.smedi.ismedi.constructionproject.api.feign.RemoteConstructionProjectFeignApi;
import com.smedi.ismedi.inmail.core.domain.dto.ProjectQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.ProjectRemoteService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class ProjectRemoteServiceImpl implements ProjectRemoteService {
    private final RemoteConstructionProjectFeignApi remoteConstructionProjectFeignApi;

    private final AdapterConverter adapterConverter;

    @Override
    public List<ProjectResp> getProjectList(ProjectQuery projectQuery) {
        ProjectListQueryRequest projectListQueryRequest = adapterConverter.projectQueryToProjectListQueryRequest(projectQuery);
        ResponseData<List<ProjectResp>> responseData = remoteConstructionProjectFeignApi.getProjectList(projectListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public List<ProjectResp> getProjectListWithOutToken(ProjectQuery projectQuery) {
        ProjectListQueryRequest projectListQueryRequest = adapterConverter.projectQueryToProjectListQueryRequest(projectQuery);
        ResponseData<List<ProjectResp>> responseData = remoteConstructionProjectFeignApi.getProjectList(projectListQueryRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public List<ProjectWithDingResp> getProjectResponseDataByOrgId(Long orgId, List<Long> projectIds) {
        ResponseData<List<ProjectWithDingResp>> responseData = remoteConstructionProjectFeignApi.getProjectResponseDataByOrgId(orgId);
        if(!responseData.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        if(CollectionUtils.isEmpty(responseData.getData())){
            return new ArrayList<>();
        }
        List<ProjectWithDingResp> returnProjects = new ArrayList<>();
        for(ProjectWithDingResp project: responseData.getData()){
            if(projectIds.contains(project.getId())){
                returnProjects.add(project);
            }
        }
        returnProjects = returnProjects.stream().filter(projectWithDingResp -> projectWithDingResp.getDingDeptKey()!=null && "CONSTRUCTING".equals(projectWithDingResp.getProjectStatus())).sorted(Comparator.comparing(ProjectWithDingResp::getId)).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(returnProjects)){
            return new ArrayList<>();
        }
        return returnProjects;
    }

    @Override
    public List<ProjectWithDingResp> getProjectResponseDataByOrgId(Long orgId) {
        ResponseData<List<ProjectWithDingResp>> responseData = remoteConstructionProjectFeignApi.getProjectResponseDataByOrgId(orgId);
        if(!responseData.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        if(CollectionUtils.isEmpty(responseData.getData())){
            return new ArrayList<>();
        }
        return responseData.getData();
    }

    @Override
    public Integer getProjectCount(ProjectQuery projectQuery) {
        ProjectCountQueryRequest projectCountQueryRequest = adapterConverter.projectQueryToProjectCountQueryRequest(projectQuery);
        ResponseData<Integer> responseData = remoteConstructionProjectFeignApi.getProjectCount(projectCountQueryRequest);
        if(!responseData.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }

    @Override
    public ProjectWithDingResp getProjectById(Long id) {
        ResponseData<ProjectWithDingResp> responseData = remoteConstructionProjectFeignApi.getProjectWithDing(id);
        if(!responseData.isSuccess()){
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), String.valueOf(responseData.getErrorMsg()));
        }
        return responseData.getData();
    }
}
